<?php

/**
 * 
 * @export Loader
 * @export-metadata singleinstance
 */
class ServiceLoader extends AbstractLoader {

    private static $instance = null;

    /**
     *
     * @return ServiceLoader
     */
    public static function instance() {
        if (self::$instance == null) {
            self::$instance = new ServiceLoader();
        }

        return self::$instance;
    }

    /**
     *
     * @param string $name
     * @return BaseService
     */
    protected function resolveObject($name) {
        $serviceName = $name . "Service";
        $this->checkFile($serviceName, Loader::toSinglePath("models::services", DS));
        
        $service = new $serviceName(ModulesContext::instance()->getConfig()->get("acawebServicesURI"));
        
        $user = $this->getCurrentUser();
        
        if (!$user->isAnonimous()) {
            $service->addHeaderVariable("Authorization", $user->getData()->get("token"));
        }

        return $service;
    }

}

?>
